package com.kinghunt.misp.web.advice;

import com.kinghunt.misp.common.exception.AnonymousUserException;
import com.kinghunt.misp.web.advice.annotation.LoginRequire;
import com.kinghunt.misp.web.context.ApiContext;
import com.kinghunt.misp.web.context.ApiUser;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoginAspect {
    @Before(value = "@annotation(loginRequire)")
    public void checkLogin(LoginRequire loginRequire) {
        ApiUser user = ApiContext.apiUser();
        long userId = user.getUserId();

        if (loginRequire.allowAnonymous()) {
            return;
        } else if (userId <= 0) {
            throw new AnonymousUserException();
        }
    }
}
